package com.atomone.epublib.model;

import com.atomone.epublib.utils.EpubUtils;
import com.atomone.epublib.utils.MD5Utils;

import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Generated;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.Transient;

import java.io.Serializable;
import java.util.List;

import nl.siegmann.epublib.domain.Book;
import nl.siegmann.epublib.domain.Metadata;

/**
 * 收藏的书籍
 *
 * @author liyayun
 */
@Entity
public class CollBookBean implements Serializable {

    public static final int STATUS_UNCACHE = 0; //未缓存
    public static final int STATUS_CACHING = 1; //正在缓存
    public static final int STATUS_CACHED  = 2;  //已经缓存

    public static final long serialVersionUID = 536871008000000L;

    @Id
    private String  _id; // 本地书籍中，path 的 md5 值作为本地书籍的 id
    private String  title;
    private String  author;
    private String  shortIntro;
    private String  cover; // 在本地书籍中，该字段作为本地文件的路径
    private boolean hasCp;
    private int     latelyFollower;
    private double  retentionRatio;
    //最新更新日期
    private String  updated;
    //最新阅读日期
    private String  lastRead;
    private int     chaptersCount;
    private String  lastChapter;
    //是否更新或未阅读
    private boolean isUpdate = true;
    //是否是本地文件
    private boolean isLocal  = false;

    @Transient
    private List<EpubChapter> mEpubChapters;
    @Transient
    private Book              mBook;

    public CollBookBean() {
    }

    public CollBookBean(Book book) {
        this.mBook = book;
        //_id
        this._id = MD5Utils.strToMd5By32(book.getTitle() + book.getMetadata().getAuthors());
        //
        Metadata metaData = book.getMetadata();
        //author
        String authorFirstName = "Unknown author";
        String authorLastName = "";
        if (metaData.getAuthors().size() > 0) {
            authorFirstName = metaData.getAuthors().get(0).getFirstname();
            authorLastName = metaData.getAuthors().get(0).getLastname();
        }
        this.author = authorFirstName + authorLastName;
        // 简介
        if (!metaData.getDescriptions().isEmpty()) {
            this.shortIntro = metaData.getDescriptions().get(0);
        }
        //
        mEpubChapters = EpubUtils.getTableOfContents(book);

        //        this.cover = cover;
        //        this.hasCp = hasCp;
        //        this.latelyFollower = latelyFollower;
        //        this.retentionRatio = retentionRatio;
        //        this.updated = updated;
        //        this.lastRead = lastRead;
        //        this.chaptersCount = chaptersCount;
        //        this.lastChapter = lastChapter;
        //        this.isUpdate = isUpdate;
        //        this.isLocal = isLocal;
    }

    @Generated(hash = 757968961)
    public CollBookBean(String _id, String title, String author, String shortIntro,
                        String cover, boolean hasCp, int latelyFollower, double retentionRatio,
                        String updated, String lastRead, int chaptersCount, String lastChapter,
                        boolean isUpdate, boolean isLocal) {
        this._id = _id;
        this.title = title;
        this.author = author;
        this.shortIntro = shortIntro;
        this.cover = cover;
        this.hasCp = hasCp;
        this.latelyFollower = latelyFollower;
        this.retentionRatio = retentionRatio;
        this.updated = updated;
        this.lastRead = lastRead;
        this.chaptersCount = chaptersCount;
        this.lastChapter = lastChapter;
        this.isUpdate = isUpdate;
        this.isLocal = isLocal;
    }

    public String get_id() {
        return _id;
    }

    public void set_id(String _id) {
        this._id = _id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getShortIntro() {
        return shortIntro;
    }

    public void setShortIntro(String shortIntro) {
        this.shortIntro = shortIntro;
    }

    public String getCover() {
        return cover;
    }

    public void setCover(String cover) {
        this.cover = cover;
    }

    public boolean isHasCp() {
        return hasCp;
    }

    public void setHasCp(boolean hasCp) {
        this.hasCp = hasCp;
    }

    public int getLatelyFollower() {
        return latelyFollower;
    }

    public void setLatelyFollower(int latelyFollower) {
        this.latelyFollower = latelyFollower;
    }

    public double getRetentionRatio() {
        return retentionRatio;
    }

    public void setRetentionRatio(double retentionRatio) {
        this.retentionRatio = retentionRatio;
    }

    public String getUpdated() {
        return updated;
    }

    public void setUpdated(String updated) {
        this.updated = updated;
    }

    public int getChaptersCount() {
        return chaptersCount;
    }

    public void setChaptersCount(int chaptersCount) {
        this.chaptersCount = chaptersCount;
    }

    public String getLastChapter() {
        return lastChapter;
    }

    public void setLastChapter(String lastChapter) {
        this.lastChapter = lastChapter;
    }

    public boolean isUpdate() {
        return isUpdate;
    }

    public void setUpdate(boolean update) {
        isUpdate = update;
    }

    public boolean getHasCp() {
        return this.hasCp;
    }

    public boolean getIsUpdate() {
        return this.isUpdate;
    }

    public void setIsUpdate(boolean isUpdate) {
        this.isUpdate = isUpdate;
    }

    public boolean isLocal() {
        return isLocal;
    }

    public void setLocal(boolean local) {
        isLocal = local;
    }

    public String getLastRead() {
        return lastRead;
    }

    public void setLastRead(String lastRead) {
        this.lastRead = lastRead;
    }

    public Book getBook() {
        return mBook;
    }

    public void setBook(Book book) {
        mBook = book;
    }

    public void setBookChapters(List<EpubChapter> beans) {
        mEpubChapters = beans;
        for (EpubChapter bean : mEpubChapters) {
            bean.setBookId(get_id());
        }
    }

    public boolean getIsLocal() {
        return this.isLocal;
    }

    public void setIsLocal(boolean isLocal) {
        this.isLocal = isLocal;
    }

    public List<EpubChapter> getBookChapters() {
        return mEpubChapters;
    }
}